
#include "mish_grad_tiling.h"
#include "register/op_def_registry.h"
#include "tiling_common.h"

namespace optiling {
static ge::graphStatus TilingFunc(gert::TilingContext *context) {

  MishGradTilingData tiling;
  auto size = set_size_by_input(tiling, context, 0);
  auto tanh_size = context->GetInputShape(2)->GetStorageShape().GetShapeSize();
  COMMON_TILING_GET_PLATFORM;
  COMMON_TILING_GET_UB_SIZE;
  auto dtype = context->GetInputDesc(0)->GetDataType();
  auto dtypeSize = GetSizeByDataType(dtype);

  context->SetBlockDim(1);

  // 分割UB
  // grad*2  x * 2, tanhx * 2 y * 2 temp;
  auto count = 2 + 2 + 2 + 2 + 1;
  if (tanh_size <= 0) {
    // tanhx
    count -= 2;
    // temp2, temp3
    count += 2;
  }
  if (dtype == ge::DataType::DT_FLOAT16) {
    // grad*2  x * 2, tanhx * 2 y * 2 temp * 2
    // grad*2  x * 2, tanhx * 2 y * 2
    count = 2 + 2 + 2 + 2 + 2;
    count += 2 + 2 + 2 + 2;
    if (tanh_size <= 0) {
      // tanhx
      count -= 2 + 2;
      // temp2 * 2, temp3 * 2
      count += 2 + 2;
    }
  }
  auto split_ub = ub_size / count / 256 * 256 / dtypeSize;
  tiling.set_size(size);
  tiling.set_tileLength(split_ub);
  tiling.set_finalLength(size % split_ub);
  tiling.set_hasTanhx(tanh_size > 0);
  std::cout << "size = " << tiling.get_size() << std::endl;
  std::cout << "tileLength = " << tiling.get_tileLength() << std::endl;
  std::cout << "finalLength = " << tiling.get_finalLength() << std::endl;
  std::cout << "hastanhx = " << tiling.get_hasTanhx() << std::endl;
  tiling_zero_workspace(context);
  tiling_save_to_buffer(tiling, context);
  return ge::GRAPH_SUCCESS;
}
} // namespace optiling

namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext *context) {
  const gert::Shape *x1_shape = context->GetInputShape(0);
  gert::Shape *y_shape = context->GetOutputShape(0);
  *y_shape = *x1_shape;
  return GRAPH_SUCCESS;
}
} // namespace ge

namespace ops {
class MishGrad : public OpDef {
public:
  explicit MishGrad(const char *name) : OpDef(name) {
    this->Input("grad")
        .ParamType(REQUIRED)
        .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
        .Format({ge::FORMAT_ND, ge::FORMAT_ND})
        .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
    this->Input("x")
        .ParamType(REQUIRED)
        .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
        .Format({ge::FORMAT_ND, ge::FORMAT_ND})
        .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
    this->Input("tanhx")
        .ParamType(OPTIONAL)
        .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
        .Format({ge::FORMAT_ND, ge::FORMAT_ND})
        .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
    this->Output("y")
        .ParamType(REQUIRED)
        .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
        .Format({ge::FORMAT_ND, ge::FORMAT_ND})
        .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});

    this->SetInferShape(ge::InferShape);

    this->AICore().SetTiling(optiling::TilingFunc);
    this->AICore().AddConfig("ascend310b");
  }
};

OP_ADD(MishGrad);
} // namespace ops
